package com.orangelife.service.feeDetail.impl;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.orangelife.exception.AppBizException;
import com.orangelife.service.feeDetail.IFeeDetailService;
import com.orangelife.service.feeDetail.modal.PropFeeDetailDto;
import com.orangelife.service.propFeeAccount.IPropFeeAccountService;
import com.orangelife.service.propFeeApartment.IPropFeeApartmentService;
import com.orangelife.service.propFeeDetailImportLog.IPropFeeDetailImportLogService;
import com.orangelife.service.propFeeDetailImportLog.model.PropFeeDetailImportLogDto;
import com.orangelife.setting.constant.TypeConst;
import com.orangelife.utils.IdUtil;
import com.orangelife.utils.StringUtils;

public class HandleFeeDetailHistoryThread extends Thread implements Runnable {

	private String threadName;
	private List<Map<String, Object>> data;
	private int start;
	private int end;
	private String commID;
	private String importType;
private int n=0;
	private List<Map<String, Object>> houseInfos;

	private IPropFeeAccountService propFeeAccountService;

	private IPropFeeApartmentService propFeeApartmentService;

	private IFeeDetailService feeDetailService;
       private ImportLogs<PropFeeDetailImportLogDto, IPropFeeDetailImportLogService> logs;

	public HandleFeeDetailHistoryThread(String commID, String threadName,
			List<Map<String, Object>> data,
			IPropFeeAccountService propFeeAccountService,
			IFeeDetailService feeDetailService,
			IPropFeeApartmentService propFeeApartmentService,
			String importType, List<Map<String, Object>> houseInfos,ImportLogs<PropFeeDetailImportLogDto, IPropFeeDetailImportLogService> logs) {
		this.threadName = threadName;
		this.data = data;
		
		this.propFeeAccountService = propFeeAccountService;
		this.feeDetailService = feeDetailService;
		this.commID = commID;
		this.logs=logs;
		this.importType = importType;
		this.propFeeApartmentService = propFeeApartmentService;
		this.houseInfos = houseInfos;

	}

	public void run() {
		try {
			if (StringUtils.isBlank(importType)) {
			}
			
			
			
			
			
		
			//更新的Map
		
			List<PropFeeDetailDto> propFeeDetailDtoDtos = new ArrayList<PropFeeDetailDto>();
			
			
			List<PropFeeDetailDto> propFeeDetails = new ArrayList<PropFeeDetailDto>();
			List<String> deleteDetails = new ArrayList<String>();
			for (Map<String, Object> map : data) {
				map.put("commID", commID);
		

		

					String apartmentID =map.get("apartmentID").toString();
					
		
							List<PropFeeDetailDto> feeDetail = feeDetailService
									.findPropFeeDetailByRelevantID(null,
											apartmentID, map
													.get("feeTypeID")
													.toString(), null, null,
											null,null,
											null, map.get("feeDate").toString());
							// 1为添加，2为删除添加，3为更新，0为不做任何操作
							int n=0;
							
							
							PropFeeDetailDto propFeeDetail1=null;
							if (feeDetail == null || feeDetail.size() <= 0) {
								
								n=1;
								
							}
							else{
								propFeeDetail1= feeDetail.get(0);
								if (StringUtils.isBlank(propFeeDetail1
										.getRuleID())) {
									if(propFeeDetail1.getIspre().equals("2")||(propFeeDetail1.getIspre().equals("1")&&propFeeDetail1.getStatus().equals("0"))){
										n=3;
								
									}else{
										logs.addErrnum();
										logs.errMap.put(Integer.parseInt(map.get("rownum").toString()),map.get("feeTypeName").toString()+" 存在已缴费的导入数据，无法插入更新");
										
									}
									
									
								}else{
									if(propFeeDetail1.getIspre().equals("2")&&propFeeDetail1.getStatus().equals("0")){
										
										n=2;
								
									}else{
										logs.addErrnum();
										logs.errMap.put(Integer.parseInt(map.get("rownum").toString()),map.get("feeTypeName").toString()+"在系统中已存在数据");
	
										
									}
									
									
								}
								
							}
							if(n==1||n==2){
								SimpleDateFormat formatter = new SimpleDateFormat(
										"yyyy-MM");
								BigDecimal money = new BigDecimal(map.get(
										"money").toString());
								Date paytime = formatter.parse(map.get(
										"feeDate").toString());
								PropFeeDetailDto propFeeDetail = new PropFeeDetailDto();
								propFeeDetail.setFeeTypeID(map.get(
										"feeTypeID").toString());
								propFeeDetail.setFeeDate(map.get("feeDate")
										.toString());
								propFeeDetail.setRealamount(money);
								// propFeeDetail.setFeeAmount(money.toString());
								// propFeeDetail.setFeePrice(money.toString());
								propFeeDetail.setTimetype("0");
								propFeeDetail.setApartmentID(apartmentID);
								propFeeDetail.setStatus(map.get("status")
										.toString());
								propFeeDetail.setType(map.get("ispre")
										.toString());
								propFeeDetail.setIspre(map.get("ispre")
										.toString());
								if ("2".equals(propFeeDetail.getType())) {
									propFeeDetail.setPaytime(paytime);

								}

								propFeeDetail.setAmount(money);
								propFeeDetail.setFeeDetailID(IdUtil.getuuId());
								propFeeDetail.setItemStat(TypeConst.FLAG_ON);
								propFeeDetail.setTscreate(new Date());
								propFeeDetail.setTsedit(new Date());
								logs.addSnum();
								propFeeDetailDtoDtos.add(propFeeDetail);
								
							}
							if(n==2){
								
								deleteDetails.add(propFeeDetail1.getFeeDetailID());
								
							}
							if(n==3){
								
								SimpleDateFormat formatter = new SimpleDateFormat(
										"yyyy-MM");
								BigDecimal money = new BigDecimal(map
										.get("money").toString());
								Date paytime = formatter.parse(map.get(
										"feeDate").toString());			

								if (money.compareTo(feeDetail.get(0)
										.getAmount()) > 0
										|| money.compareTo(feeDetail.get(0)
												.getAmount()) < 0) {
									propFeeDetail1.setAmount(money);
									propFeeDetail1.setRealamount(money);
									propFeeDetail1.setTsedit(new Date());
								
									propFeeDetails.add(propFeeDetail1);
								}
								logs.addUnum();
							}

				
				
				

			}
			if (deleteDetails != null && deleteDetails.size() > 0) {

				/***
				 * 批量删除
				 */
				feeDetailService.deleteFeeDetails(deleteDetails);
			}
			if (propFeeDetailDtoDtos != null && propFeeDetailDtoDtos.size() > 0) {

				/***
				 * 批量添加
				 */
				feeDetailService.addPropFeeDetail(propFeeDetailDtoDtos);
			}
			if (propFeeDetails != null && propFeeDetails.size() > 0) {

				/***
				 * 批量更新
				 */
				feeDetailService.editPropFeeDetail(propFeeDetails);
				
				
				
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try {
			if(logs.checkIsOk(this.threadName)){
				
				
				logs.getImportLog().setStatus("2");
				logs.getImportLog().setImportInfo("成功插入"+logs.snum+"条,"+"更新"+logs.unum+"条,"+"失败"+logs.errnum+"条");
				String logurl =logs.getErrUrl();
				logs.getImportLog().setLink(logurl);
				logs.getImportLog().setFinishTime(new Date());
				logs.getImportLog().setTsedit(new Date());
				logs.getImportLog().setRemark("导入成功");
				logs.getImportLog().setCommID(commID);
			
					logs.getImportService()
							.editPropFeeDetailImportLog(logs.getImportLog());
			
				
			}
			
			} catch (AppBizException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} finally{
				
				
				ImportFeeDetailHistoryImpl.setFeeStatusUnlock(commID);
			}
		}
	}
}
